package com.projectplace.GUI.Callback;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.lang.reflect.Type;
import java.util.List;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.projectplace.GUI.Callback.CacheCallbackInterface.ReponseCompareConversations;
import com.projectplace.GUI.Callback.CacheCallbackInterface.ResponseConversation;
import com.projectplace.GUI.Callback.CacheCallbackInterface.ResponseMyProjects;
import com.projectplace.GUI.Callback.CacheCallbackInterface.ResponseProjectConversation;
import com.projectplace.api.objectResponses.Project;
import com.projectplace.api.objectResponses.Conversation.Conversation;



public class CacheHelperCallbackWrapper implements Runnable{

	private static CacheCallbackInterface cacheCallback;
	private static String response;
	private static final Gson gson = new Gson();
	private static Error err;
	
	public CacheHelperCallbackWrapper(CacheCallbackInterface callbackActivity) {
		CacheHelperCallbackWrapper.cacheCallback = callbackActivity;
		
	}
	
	public CacheHelperCallbackWrapper(CacheCallbackInterface callbackActivity,Error err){
		CacheHelperCallbackWrapper.cacheCallback = callbackActivity;
		this.err = err;
	}
	
	public void setResponse(String response) {
		CacheHelperCallbackWrapper.response = response;
		
	}
	
	
	public void setError(Error err){
		CacheHelperCallbackWrapper.err = err;
	}
	
	public void run() {
		cacheCallback.onFail(err);
	}
	
	public static class loadMyProjects extends CacheHelperCallbackWrapper{
		
		private final String LOG_TAG = "loadMyProjects";
		
		public loadMyProjects(
				CacheCallbackInterface.ResponseMyProjects callbackActivity) {
			super(callbackActivity);
		}
		public void run() {
			Log.d(LOG_TAG, "Posting");
			Type listType = new TypeToken<List<Project>>() {}.getType();
			List<Project> fromJson = gson.fromJson(response, listType);
			((ResponseMyProjects) cacheCallback).onReceive(fromJson); 
		}
	}
	
	public static class loadProjectConversation extends CacheHelperCallbackWrapper{
		
		private final String LOG_TAG = "loadProjectConversation";
		
		public loadProjectConversation(
				CacheCallbackInterface.ResponseProjectConversation callbackActivity) {
			super(callbackActivity);
		}
		public void run() {
			Log.d(LOG_TAG, "Posting");
			Type listType = new TypeToken<List<Conversation>>() {}.getType();
			List<Conversation> fromJson = gson.fromJson(response, listType);
			if(fromJson != null)
				((ResponseProjectConversation) cacheCallback).onReceive(fromJson); 
			else
				((ResponseProjectConversation) cacheCallback).onFail(new Error("Nullpointer!!!!"));
		}
	}
	
	public static class loadConversation extends CacheHelperCallbackWrapper{
		
		private final String LOG_TAG = "loadConversation";
		
		public loadConversation(
				CacheCallbackInterface.ResponseConversation callbackActivity) {
			super(callbackActivity);
		}
		public void run() {
			Log.d(LOG_TAG, "Posting");
			Conversation fromJson = gson.fromJson(response, Conversation.class);
			((ResponseConversation) cacheCallback).onReceive(fromJson); 
		}
	}
	
	
public static class compareConversationsList extends CacheHelperCallbackWrapper{
		
		private final String LOG_TAG = "loadConversation";
		private List<Conversation> compareResponse;
		
		public compareConversationsList(
				CacheCallbackInterface.ReponseCompareConversations callbackActivity,List<Conversation> compareResponse) {
			super(callbackActivity);
			this.compareResponse = compareResponse;
		}
		public void run() {
			Log.d(LOG_TAG, "Posting");
			((ReponseCompareConversations) cacheCallback).onReceive(compareResponse); 
		}
	}
	
}
